define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init();
            this.table.first();
            this.table.second();
        },

        table: {
            first: function () {
                // 表格1
                var table1 = $("#table1");
                table1.bootstrapTable({
                    url: 'noipfraud/index',
                    toolbar: '#toolbar1',
                    sortName: 'id',
                    sortOrder:'asc',
                    search: false,
                    columns: [
                        [
                            {field: 'name', title: __('网站'), operate: 'LIKE'},
                            {
                                field: 'operate', title: __('Operate'), table: table1, events: Table.api.events.operate, buttons: [
                                {
                                    name: 'log',
                                    title: '广告列表',
                                    text: '广告列表',
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-click',
                                    click: function (e, data) {
                                        $("#myTabContent2 .form-commonsearch input[name='noipfraud_name']").val(data.name);
                                        $("#myTabContent2 .btn-refresh").trigger("click");
                                    }
                                }
                            ], formatter: Table.api.formatter.operate
                            }
                        ]
                    ]
                });

                // 为表格1绑定事件
                Table.api.bindevent(table1);
            },
            second: function () {
                // 表格2
                var table2 = $("#table2");
                table2.bootstrapTable({
                    url: 'noipfraudinfo/index',
                    extend: {
                        index_url: '',
                        add_url: '',
                        edit_url: '',
                        del_url: '',
                        multi_url: '',
                        table: '',
                    },
                    toolbar: '#toolbar2',
                    sortName: 'updatetimes',
                    sortOrder:'desc',
                    search: false,
                    searchFormVisible: true,
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'noipfraud_name', title: __('Noipfraud_name')},
                            {field: 'name', title: __('Name'), operate: 'LIKE'},
                            {field: 'url', title: __('Url'), operate: 'LIKE', formatter: Table.api.formatter.url},
                            //{field: 'fakeurl', title: __('Fakeurl'), operate: 'LIKE', formatter: Table.api.formatter.url},
                            {field: 'info', title: __('Info'), operate: 'LIKE'},
                            {field: 'textinfo', title: __('采集链接'), operate: 'LIKE', formatter: Table.api.formatter.url},
                            {field: 'updatetimes', title: __('最后修改时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        ]
                    ]
                });

                // 为表格2绑定事件
                Table.api.bindevent(table2);

                //导入godaddy活跃域名
                $(document).on('click', '.btn-noipfrauinfo', function () {
                    $.ajax({
                        dataType: 'json',
                        type: 'POST',
                        url: "Noipfraudinfo/updates",
                        data: {},
                        success: function (json) {
                            console.log('json',json);
                            Layer.closeAll();
                            Layer.alert('更新完成');
                            //刷新父级页面
                            window.parent.location.reload();
                            //刷新当前页面
                            table.bootstrapTable('refresh', {});
                            return true;
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            Layer.alert('更新失败');
                        }
                    });
                });
            }
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});